﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="TestSubtractIndexedDenseSymmetricMatrix.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential.Tests.DenseSymmetricMatrixTest.DoubleExtensions
{
    #region Usings

    using System;
    using System.Globalization;

    using ExulLibrary.Exceptions;
    using ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions;

    using NUnit.Framework;

    using Extensions = ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions.DenseSymmetricMatrixExtensions;

    #endregion

    /// <summary>
    ///     Test suite for
    ///     <see cref="Extensions.Subtract(DenseSymmetricMatrix{double},int[],int[],DenseSymmetricMatrix{double})"/>.
    /// </summary>
    [TestFixture]
    [Category("ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions.DenseSymmetricMatrixExtensions")]
    public sealed class TestSubtractIndexedDenseSymmetricMatrix
    {
        /// <summary>
        ///     Tests the method when indexes of minuend elements are non-negative and less than
        ///     size of dimensions of minuend matrix.
        /// </summary>
        [Test]
        public void WhenMinuendIndexesAreValid()
        {
            // Arrange
            var minuendData = new double[] { 10, 20, 30, 40, 50, 60 };
            var minuend = new DenseSymmetricMatrix<double>(minuendData);
            var subtrahendData = new double[] { 1, 2, 3 };
            var subtrahend = new DenseSymmetricMatrix<double>(subtrahendData);
            var minuendRowsIndexes = new[] { 0, 2 };
            var minuendColumnsIndexes = new[] { 0, 1 };
            var expectedResult = new double[] { 9, 20, 30, 38, 47, 60 };

            // Act
            minuend.Subtract(minuendRowsIndexes, minuendColumnsIndexes, subtrahend);

            // Assert
            Assert.That(minuend.Values, Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when collection of rows indexes of minuend elements is empty.
        /// </summary>
        [Test]
        public void WhenMinuendRowsIndexesCollectionIsEmpty()
        {
            // Arrange
            var minuendData = new double[] { 10, 20, 30, 40, 50, 60 };
            var minuend = new DenseSymmetricMatrix<double>(minuendData);
            var subtrahendData = new double[] { 1, 2, 3 };
            var subtrahend = new DenseSymmetricMatrix<double>(subtrahendData);
            var minuendRowsIndexes = new int[0];
            var minuendColumnsIndexes = new[] { 0, 1 };
            var expectedResult = new double[] { 10, 20, 30, 40, 50, 60 };

            // Act
            minuend.Subtract(minuendRowsIndexes, minuendColumnsIndexes, subtrahend);

            // Assert
            Assert.That(minuend.Values, Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when collection of columns indexes of minuend elements is empty.
        /// </summary>
        [Test]
        public void WhenAugendColumnsIndexesCollectionIsEmpty()
        {
            // Arrange
            var minuendData = new double[] { 10, 20, 30, 40, 50, 60 };
            var minuend = new DenseSymmetricMatrix<double>(minuendData);
            var subtrahendData = new double[] { 1, 2, 3 };
            var subtrahend = new DenseSymmetricMatrix<double>(subtrahendData);
            var minuendRowsIndexes = new[] { 0, 2 };
            var minuendColumnsIndexes = new int[0];
            var expectedResult = new double[] { 10, 20, 30, 40, 50, 60 };

            // Act
            minuend.Subtract(minuendRowsIndexes, minuendColumnsIndexes, subtrahend);

            // Assert
            Assert.That(minuend.Values, Is.EquivalentTo(expectedResult));
        }

#if SAFE

        /// <summary>
        ///     Tests the method when minuend is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenTargetIsNull()
        {
            // Arrange
            var subtrahendData = new double[] { 1, 2, 3 };
            var subtrahend = new DenseSymmetricMatrix<double>(subtrahendData);
            var minuendRowsIndexes = new[] { 0, 2 };
            var minuendColumnsIndexes = new[] { 0, 1 };

            // Act and Assert
            Assert.That(
                () => DenseSymmetricMatrixExtensions.Subtract(null, minuendRowsIndexes, minuendColumnsIndexes, subtrahend),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("minuend"));
        }

        /// <summary>
        ///     Tests the method when minuend rows indexes collection is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenMinuendRowsIndexesIsNull()
        {
            // Arrange
            var minuendData = new double[] { 10, 20, 30, 40, 50, 60 };
            var minuend = new DenseSymmetricMatrix<double>(minuendData);
            var subtrahendData = new double[] { 1, 2, 3 };
            var subtrahend = new DenseSymmetricMatrix<double>(subtrahendData);
            var minuendColumnsIndexes = new[] { 0, 1 };

            // Act and Assert
            Assert.That(
                () => minuend.Subtract(null, minuendColumnsIndexes, subtrahend),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("minuendRowsIndexes"));
        }

        /// <summary>
        ///     Tests the method when minuend columns indexes collection is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendColumnsIndexesIsNull()
        {
            // Arrange
            var minuendData = new double[] { 10, 20, 30, 40, 50, 60 };
            var minuend = new DenseSymmetricMatrix<double>(minuendData);
            var subtrahendData = new double[] { 1, 2, 3 };
            var subtrahend = new DenseSymmetricMatrix<double>(subtrahendData);
            var minuendRowsIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => minuend.Subtract(minuendRowsIndexes, null, subtrahend),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("minuendColumnsIndexes"));
        }

        /// <summary>
        ///     Tests the method when subtrahend is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenSubtrahendIsNull()
        {
            // Arrange
            var minuendData = new double[] { 10, 20, 30, 40, 50, 60 };
            var minuend = new DenseSymmetricMatrix<double>(minuendData);
            var minuendRowsIndexes = new[] { 0, 2 };
            var minuendColumnsIndexes = new[] { 0, 1 };

            // Act and Assert
            Assert.That(
                () => minuend.Subtract(minuendRowsIndexes, minuendColumnsIndexes, null),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("subtrahend"));
        }

        /// <summary>
        ///     Tests the method when a minuend row index is negative or greater than or equal to size of minuend matrix.
        /// </summary>
        /// <param name="position">
        ///     The position of index.
        /// </param>
        /// <param name="minuendRowIndex">
        ///     The row index of minuend element.
        /// </param>
        [Test]
        public void WhenAMinuendRowIndexIsOutOfRange([Values(0, 1)] int position, [Values(-1, 3)] int minuendRowIndex)
        {
            // Arrange
            var minuendData = new double[] { 10, 20, 30, 40, 50, 60 };
            var minuend = new DenseSymmetricMatrix<double>(minuendData);
            var subtrahendData = new double[] { 1, 2, 3 };
            var subtrahend = new DenseSymmetricMatrix<double>(subtrahendData);
            var minuendRowsIndexes = new int[2];
            minuendRowsIndexes[position] = minuendRowIndex;

            var minuendColumnsIndexes = new[] { 0, 1 };

            // Act and Assert
            var parameterName = string.Format(CultureInfo.InvariantCulture, "minuendRowsIndexes[{0}]", position);
            Assert.That(
                () => minuend.Subtract(minuendRowsIndexes, minuendColumnsIndexes, subtrahend),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(parameterName));
        }

        /// <summary>
        ///     Tests the method when a minuend column index is negative or greater than or equal to size of minuend matrix.
        /// </summary>
        /// <param name="position">
        ///     The position of index.
        /// </param>
        /// <param name="augendColumnIndex">
        ///     The column index of minuend element.
        /// </param>
        [Test]
        public void WhenAMinuendColumnIndexIsOutOfRange([Values(0, 1)] int position, [Values(-1, 3)] int augendColumnIndex)
        {
            // Arrange
            var minuendData = new double[] { 10, 20, 30, 40, 50, 60 };
            var minuend = new DenseSymmetricMatrix<double>(minuendData);
            var subtrahendData = new double[] { 1, 2, 3 };
            var subtrahend = new DenseSymmetricMatrix<double>(subtrahendData);
            var minuendRowsIndexes = new[] { 0, 2 };
            var minuendColumnsIndexes = new int[2];
            minuendColumnsIndexes[position] = augendColumnIndex;

            // Act and Assert
            var parameterName = string.Format(CultureInfo.InvariantCulture, "minuendColumnsIndexes[{0}]", position);
            Assert.That(
                () => minuend.Subtract(minuendRowsIndexes, minuendColumnsIndexes, subtrahend),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(parameterName));
        }

#endif
    }
}